var express = require("express");
var router = express.Router();
// 配置图片上虚拟路由
router.use("/upload", express.static("upload"));

var {work_experienceModel} = require('../model/model')

// 添加工作经历
router.post("/add_work", async (req, res) => {
    let body = req.body;
    await work_experienceModel.create(body);
    res.send({
      code: 200,
      msg: "添加成功",
    });
  });

  // 修改工作经历
router.put("/edit_work/:id", async function (req, res) {
    let body = req.body;
    await work_experienceModel.updateOne({ _id: req.params.id }, { $set: body });
    res.send({
      code: 200,
      msg: "修改成功",
    });
  });

  // 删除工作经历
router.delete("/del_work/:id", async function (req, res) {
    await work_experienceModel.deleteOne({ _id: req.params.id });
    res.send({
      code: 200,
      msg: "删除成功",
    });
  });
  
  // 工作经历列表
router.get("/work_list", async (req, res) => {
    let pageIndex = req.query.pageIndex;
    let pageSize = req.query.pageSize;
    let danwei_name = req.query.danwei_name;
    console.log(req.query);
    let list = [];
    let total = 0;
    let namereg = new RegExp(danwei_name, "i");
    list = await work_experienceModel.find({
      $and: [{ danwei_name: namereg }],
    })
      .skip((pageIndex - 1) * pageSize)
      .limit(pageSize)
      .sort({ times: -1 })
      .exec();
    total = list.length;
    res.send({
      code: 200,
      msg: "展示成功",
      data: list,
      total: total,
    });
  });

  


module.exports = router;
